home *** CD-ROM | disk | FTP | other *** search
- /************************************************************
- *
- *
- * Unit containing instruction code helping routines.
- *
- * by Adrian Bool in cooperation with Graham Cox.
- *
- * copyright © phantasm coding 1992.
- *
- *
- ************************************************************/
-
- #include "Global.h"
- #include "Code.h"
-
- #include "CodeHelp.proto.h"
-
- opcode getOpcode(char *theText)
- {
- short x;
- short found;
-
- found = false;
-
- for (x=1 ; x <= noOfOpcodes ; x++)
- if (strcmp(theText,pdp8[x].mnemonic) == 0)
- {
- found = true;
- break;
- }
-
- if (found)
- return(x);
- else
- {
- AsmError = NoSuchOpcode;
- return(0);
- }
- }
-
-
- void makeMemoryInstruction(opcode theInstruction,rBlock theResult,pHandle theProgram)
- {
- short theAddress;
- str255 theOperand;
-
- theResult[0] = pdp8[theInstruction].iNumber << 9; /* enter instuction code in top 4 bits */
- getSection(theProgram,theOperand);
- if (!AsmError)
- {
- theAddress = evaluateAddress(theProgram,theOperand);
- theResult[0] |= theAddress; /* and put the address in bottom 7 bits */
- }
- }
-
-
- addressType evaluateAddress(pHandle theProgram , char *theSegment)
- {
- char x;
- str255 aSeection;
- addressType theValue,
- theAddress;
-
- theAddress = 0;
-
- if (theSegment[0] == '*')
- {
- theAddress |= (1 << 8); /* set indirection bit */
- theSegment++;
- }
-
- theValue = evaluateValue(theProgram,theSegment);
-
- if (theValue < 128)
- { /* its zero page */
- theAddress |= (theValue & 127); /* allow only bottom 6 bits through */
- }
- else
- {
- if (pageOf(theValue) == thisPage(theProgram))
- {
- theAddress |= (theValue & 127);
- theAddress |= (1 << 7); /* set present-page bit */
- }
- else
- AsmError = AddressOutOfRange;
- }
-
- return(theAddress);
- }
-
- wordType evaluateValue(pHandle theProgram , char *theSegment)
- {
- str255 thePart;
- short partPosition;
- long total,value;
- operator theOperator;
-
- partPosition = 0;
-
- do
- {
- theOperator = getPart(theSegment,&partPosition,thePart);
- if (thePart[0] != '\0')
- {
- value = evaluatePart(theProgram,thePart);
- if (!AsmError)
- {
- switch (theOperator)
- {
- case None : total = value;
- break;
- case Add : total += value;
- break;
- case Subtract : total -= value;
- break;
- case RevSubtract : total = value - total;
- break;
- case Multiply : total *= value;
- break;
- case Divide :
- if (value > 0)
- total /= value;
- else
- AsmError = DivisionByZero;
- break;
- case RevDivide :
- if (total > 0)
- total = value / total;
- else
- AsmError = DivisionByZero;
- break;
- case And : total &= value;
- break;
- case Or : total |= value;
- break;
- case Not : total = (!value);
- break;
- }
- }
- }
- }
- while ((theOperator != End) && !AsmError);
-
- if ((total > 4095) && !AsmError)
- {
- AsmError = BadNumber;
- return(0);
- }
- else
- return(total);
-
- return(0);
- }
-
-
- wordType evaluatePart(pHandle theProgram , char *thePart)
- {
- wordType theValue;
-
- if (isalpha(thePart[0]))
- theValue = getLabelValue((*theProgram)->labelHeader,thePart);
- else
- {
- if (isdigit(thePart[0]))
- theValue = atoi(thePart);
- else
- {
- if (thePart[0] == '%')
- theValue = otoi(&thePart[1]);
- else
- {
- if (thePart[0] == '$')
- theValue = htoi(&thePart[1]);
- else
- AsmError = SyntaxError;
- }
- }
- }
- return(theValue);
- }
-
-
- wordType htoi(char *theText)
- {
- long total;
- short chValue;
- short noOfDigits,x;
-
- total = 0;
- noOfDigits = strlen(theText);
- for (x = 0 ; x < noOfDigits ; x++)
- {
- if (isdigit(theText[x]))
- chValue = theText[x] - 0x30;
- if (isalpha(theText[x]))
- {
- if (theText[x] <= 0x66)
- chValue = theText[x] - 0x57;
- else
- {
- AsmError = BadHex;
- return(0);
- }
- }
- total = (total *16) + chValue;
- }
- return(total);
- }
-
- wordType otoi(char *theText)
- {
- long total;
- short chValue;
- short noOfDigits,x;
-
- total = 0;
- noOfDigits = strlen(theText);
- for (x = 0 ; x < noOfDigits ; x++)
- {
- if (isdigit(theText[x]))
- {
- if (theText[x] < 0x38)
- chValue = theText[x] - 0x30;
- else
- AsmError = BadOct;
- }
- total = (total *8) + chValue;
- }
- return(total);
- }
-
- short thisPage(pHandle theProgram)
- {
- return(pageOf((*(*theProgram)->objectCode)->address));
- }
-
-
- short pageOf(addressType anAddress)
- {
- short page;
-
- page = anAddress & 0xf80; /* mask out all except page info */
- page >>= 7; /* move to begining of the integer */
- return(page);
- }
-